package models;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import util.ImageUtils;

import controllers.Game;

@SuppressWarnings("serial")
public class Minimap extends JPanel{
	
	private ImageUtils imgUtils = new ImageUtils();
	public BufferedImage img;

	public Minimap(){
		this.setLayout(new FlowLayout());
	}
	
	
	public void createMinimap(Game game){
		int w = game.stage.getNumXTiles()+2;
		int h = game.stage.getNumYTiles();
		img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		
		int[] pixels = new int[w * h];
		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				int i = x + y * w;
				
				if(game.entityIsInTile(x, y) == 2){
					pixels[i] = 0xf80000;
				}else if (game.entityIsInTile(x, y) == 1){
					pixels[i] = 0x0000f8;
				}else if (game.tentIsInTile(x, y) == 2){
					pixels[i] = 0xFF6600;
				}else if (game.tentIsInTile(x, y) == 1){
					pixels[i] = 0x99CCFF;
				}else{
					pixels[i] = 0x208020;
				}
			}
		}
		
		img.setRGB(0, 0, w, h, pixels, 0, w);
		
		Image imgAux;
		int wAux, hAux;
		
		if(w >= 2*h){
			wAux = Game.WIDTH/2;
			hAux = ((Game.WIDTH/2)*h)/w;
		}else{
			wAux = ((Game.HEIGHT-Game.HEIGHTSTAGE)*w)/h;
			hAux = Game.HEIGHT-Game.HEIGHTSTAGE;
		}
		
		imgAux = img.getScaledInstance(wAux, hAux, Image.SCALE_AREA_AVERAGING);
		
		img = imgUtils.imageToBufferedImage(imgAux);
		
	}

	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(img, 0, 0, null);
	}
	
}
